//
//  ViewController.m
//  结构
//
//  Created by 看着太阳吃烧饼 on 15/7/22.
//  Copyright (c) 2015年 看着太阳吃烧饼. All rights reserved.
//

#import "ViewController.h"
#import "imageModal.h"
#import "imageCell.h"
#import "DataService.h"
#import "BaseNavViewController.h"
#import "PhotoViewController.h"
#define kwidth [UIScreen mainScreen].bounds.size.width
#define kheight [UIScreen mainScreen].bounds.size.height
@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self _createCollectionView];
    [self _loadData];
    
    // Do any additional setup after loading the view.
}

- (void)_createCollectionView {
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.itemSize = CGSizeMake((kwidth-50)/4, (kwidth-50)/4);
    layout.minimumInteritemSpacing = 10;
    layout.minimumLineSpacing = 10;
    _collectionView = [[UICollectionView alloc] initWithFrame: self.view.bounds collectionViewLayout:layout];
    
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    [self.view addSubview:_collectionView];
    
    [_collectionView registerClass:[imageCell class] forCellWithReuseIdentifier:@"cell"];
}

- (void)_loadData {
    _imageModalArray = [[NSMutableArray alloc] init];
    
    NSArray *imageArray = [DataService getJsonDataFromFile:@"image_list.json"];
    for (NSDictionary *dic in imageArray) {
        imageModal *modal = [[imageModal alloc] init];
        [modal setValuesForKeysWithDictionary:dic];
        [_imageModalArray addObject:modal];
    }
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return _imageModalArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    imageCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    cell.modal = _imageModalArray[indexPath.row];
    return cell;
}


- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    return UIEdgeInsetsMake(10, 10, 10, 10);
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    NSMutableArray *imageUrlArray = [[NSMutableArray alloc] init];

    PhotoViewController *vc = [[PhotoViewController alloc] init];
    
    vc.currentIndex = indexPath.row;
    
    for (int i = 0; i<_imageModalArray.count; i++) {
        imageModal *modal = _imageModalArray[i];
        NSString *urlStr = modal.image;
        [imageUrlArray addObject:urlStr];
    }
    vc.view.backgroundColor = [UIColor redColor];
    vc.imageUrlArray = imageUrlArray;
    
    BaseNavViewController *nav = [[BaseNavViewController alloc] initWithRootViewController:vc];
    //剥离
    
    
    [self presentViewController:nav animated:YES completion:nil];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
